package com.ruoyi.app.api.interfaces

import com.drake.net.Net
import com.drake.net.NetConfig
import com.drake.net.Post
import com.drake.net.exception.ConvertException
import com.drake.net.exception.DownloadFileException
import com.drake.net.exception.HttpFailureException
import com.drake.net.exception.NetConnectException
import com.drake.net.exception.NetException
import com.drake.net.exception.NetSocketTimeoutException
import com.drake.net.exception.NoCacheException
import com.drake.net.exception.RequestParamsException
import com.drake.net.exception.ResponseException
import com.drake.net.exception.ServerResponseException
import com.drake.net.exception.URLParseException
import com.drake.net.interfaces.NetErrorHandler
import com.ruoyi.app.R
import com.ruoyi.app.activity.LoginActivity
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.code.Frame
import com.ruoyi.code.utils.ActivityManager
import com.ruoyi.code.witget.toast
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.UnknownHostException
import java.util.*

class ErrorHandler : NetErrorHandler {

    @OptIn(DelicateCoroutinesApi::class)
    override fun onError(e: Throwable) {
        if (e is ResponseException) {
            when (e.tag) {
                ConfigApi.TOKEN_ERROR -> {
                    toast(e.message)
                    // Token失效跳转到登录界面
                    MMKV.defaultMMKV().remove("token")
                    GlobalScope.launch {
                        Post<String>(ConfigApi.logout).await()
                    }
                    LoginActivity.startActivity(ActivityManager.instance.getTopActivity())
                }

                ConfigApi.API_ERROR -> {
                    toast(e.message)
                }

                else -> {
                    val message = when (e) {
                        is UnknownHostException -> Frame.getString(com.drake.net.R.string.net_host_error)
                        is URLParseException -> Frame.getString(com.drake.net.R.string.net_url_error)
                        is NetConnectException -> Frame.getString(com.drake.net.R.string.net_connect_error)
                        is NetSocketTimeoutException -> Frame.getString(com.drake.net.R.string.net_connect_timeout_error)
                        is DownloadFileException -> Frame.getString(com.drake.net.R.string.net_download_error)
                        is ConvertException -> Frame.getString(com.drake.net.R.string.net_parse_error)
                        is RequestParamsException -> Frame.getString(com.drake.net.R.string.net_request_error)
                        is ServerResponseException -> Frame.getString(com.drake.net.R.string.net_server_error)
                        is NullPointerException -> Frame.getString(com.drake.net.R.string.net_null_error)
                        is NoCacheException -> Frame.getString(com.drake.net.R.string.net_no_cache_error)
                        is HttpFailureException -> Frame.getString(com.drake.net.R.string.request_failure)
                        is NetException -> Frame.getString(com.drake.net.R.string.net_error)
                        else -> Frame.getString(com.drake.net.R.string.net_other_error)
                    }
                    toast(message)
                }
            }
        }
    }

}